package com.atguigu.tingshu.order.startegy.impl;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.startegy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class VipOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        //根据付款项目id查询vip服务配置
        Long itemId = tradeVo.getItemId();
        VipServiceConfig vipServiceConfig = vipServiceConfigFeignClient.findById(itemId).getData();

        //创建订单对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_VIP);

        //设置相关金额数据
        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal derateAmount = vipServiceConfig.getDiscountPrice().subtract(vipServiceConfig.getDiscountPrice());
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);

        //构建明细数据
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(orderAmount);
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        //构建订单的减免明细
        if(orderInfoVo.getDerateAmount().intValue() > 0){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("VIP折扣");

            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }

        this.setOtherDataToOrderInfoVo(orderInfoVo,stringRedisTemplate);
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}
